import { GBox } from "../containers/GBox";
import { Button } from "../../../fl/controls/Button";
import { UICompomentPool } from "../utils/UICompomentPool";
import { GButton } from "./GButton";
import { InvalidationType } from "../../../fl/core/InvalidationType";
import { GBoxDirection } from "../containers/globalVariable/GBoxDirection";
import { MuiEvent } from "../events/MuiEvent";
import { IDispose } from "../../gengine/core/IDispose";
import { Vector } from "../../../__AS3__/vec/Vector";
type int = number;
//class GTabBar
    
    export  class GTabBar extends GBox
    {
        constructor()
        {
            
            super();this.allButtons = new Vector.<Button>();
            return;
        }

        protected /* override */ createChildren(): void
        {
            super.createChildren();
            return;
        }

        protected /* override */ updateDisplayList(): void
        {
            var loc1=null;
            var loc2=null;
            if (this._dataProviderChange) 
            {
                this.reset();
                var loc3=0;
                var loc4=this.dataProvider;
                for(loc1 of loc4) 
                {
                    loc2 = UICompomentPool.getUICompoment(GButton);
                    loc2.name = loc1.name;
                    loc2.width = this.buttonWidth;
                    loc2.height = this.buttonHeight;
                    loc2.styleName = this.buttonStyleName;
                    loc2.textField.multiline = true;
                    if (!(this._labelName == "") && !(this._labelName == null)) 
                    {
                        loc2.label = loc1[this._labelName];
                    }
                    else 
                    {
                        loc2.label = loc1.label;
                    }
                    this.allButtons.push(loc2);
                    loc2.configEventListener(flash.events.MouseEvent.CLICK, this.btnClickHandler, false, 0, true);
                    if (this._buttonFilters) 
                    {
                        loc2.textField.filters = this._buttonFilters;
                    }
                    this.addChild(loc2);
                }
                this.checkSelected();
                this._dataProviderChange = false;
            }
            if (this._selectedChange) 
            {
                this._selectedChange = false;
                this.checkSelected();
            }
            super.updateDisplayList();
            return;
        }

        protected checkSelected(): void
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < numChildren) 
            {
                loc1 = getChildAt(loc2) as Button;
                if (this.selectedIndex != loc2) 
                {
                    if (!loc1.enabled) 
                    {
                        loc1.enabled = true;
                    }
                }
                else 
                {
                    loc1.enabled = false;
                }
                loc1.drawNow();
                ++loc2;
            }
            return;
        }

        public set dataProvider(arg1: Array<any>)
        {
            if (this._dataProvider == arg1) 
            {
                return;
            }
            this._dataProvider = arg1;
            this._dataProviderChange = true;
            this.invalidate(InvalidationType.ALL);
            return;
        }

        public /* override */ invalidate(arg1: string="all", arg2: Boolean=true): void
        {
            this._dataProviderChange = true;
            super.invalidate(arg1, arg2);
            return;
        }

        public get dataProvider(): Array<any>
        {
            return this._dataProvider;
        }

        protected /* override */ updateSize(): void
        {
            super.updateSize();
            this.width = direction != GBoxDirection.HORIZONTAL ? this.buttonWidth : numChildren * (this.buttonWidth + horizontalGap);
            this.height = direction != GBoxDirection.HORIZONTAL ? numChildren * (this.buttonHeight + verticalGap) : this.buttonHeight;
            return;
        }

        protected btnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            (arg1.target as Button).enabled = false;
            var loc1=this.getChildIndex(arg1.target as Button);
            if (!(this._selectedIndex == loc1) && this._selectedIndex >= 0 && this._selectedIndex < numChildren) 
            {
                (this.getChildAt(this._selectedIndex) as Button).enabled = true;
            }
            this._selectedIndex = loc1;
            dispatchEvent(new MuiEvent(MuiEvent.GTABBAR_SELECTED_CHANGE, loc1));
            return;
        }

        public set selectedIndex(arg1: int)
        {
            if (this._selectedIndex != arg1) 
            {
                this._selectedIndex = arg1;
                this._selectedChange = true;
                this.invalidate();
            }
            return;
        }

        public get selectedIndex(): int
        {
            return this._selectedIndex;
        }

        public get selectedItem(): Object
        {
            if (this._dataProvider && this._dataProvider.length > this._selectedIndex) 
            {
                return this._dataProvider[this._selectedIndex];
            }
            return null;
        }

        protected reset(): void
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < this.allButtons.length) 
            {
                loc1 = this.allButtons[loc2];
                loc1.textField.filters = [];
                loc1.removeEventListener(flash.events.MouseEvent.CLICK, this.btnClickHandler);
                if (typeof loc1 === "idispose") 
                {
                    (loc1 as IDispose).dispose();
                }
                ++loc2;
            }
            this.allButtons = new Vector.<Button>();
            return;
        }

        public set buttonStyleName(arg1: string)
        {
            this._buttonStyleName = arg1;
            this.invalidate(InvalidationType.STYLES);
            return;
        }

        public get buttonStyleName(): string
        {
            return this._buttonStyleName;
        }

        public /* override */ dispose(arg1: Boolean=true): void
        {
            this.reset();
            this._dataProvider = [];
            this._dataProviderChange = false;
            this.buttonFilters = [];
            this.buttonStyleName = "TabButton";
            this._selectedIndex = 0;
            super.dispose(arg1);
            return;
        }

        public set buttonWidth(arg1: int)
        {
            this._buttonWidth = arg1;
            return;
        }

        public get buttonWidth(): int
        {
            return this._buttonWidth;
        }

        public set buttonHeight(arg1: int)
        {
            this._buttonHeight = arg1;
            return;
        }

        public get buttonHeight(): int
        {
            return this._buttonHeight;
        }

        public set buttonFilters(arg1: Array<any>)
        {
            if (this._buttonFilters == arg1) 
            {
                return;
            }
            this._buttonFilters = arg1;
            return;
        }

        public get buttonFilters(): Array<any>
        {
            return this._buttonFilters;
        }

        public set labelName(arg1: string)
        {
            this._labelName = arg1;
            return;
        }

        public getButtonAt(arg1: int): Button
        {
            if (arg1 < this.allButtons.length) 
            {
                return this.allButtons[arg1];
            }
            return null;
        }

        protected /* var */_dataProvider: Array<any>;

        protected /* var */_dataProviderChange: Boolean=false;

        protected /* var */_selectedIndex: int=0;

        protected /* var */_selectedChange: Boolean=false;

        protected /* var */_buttonStyleName: string="TabButton";

        protected /* var */_buttonWidth: int=67;

        protected /* var */_buttonFilters: Array<any>;

        protected /* var */allButtons: Vector.<Button>;

        private /* var */_labelName: string="";

        protected /* var */_buttonHeight: int=22;
    }
